package com.tzxs.pay.util;


import android.util.Log;

import com.google.gson.Gson;
import com.tzxs.pay.util.netnavigator.NetNavigator;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RxUtil {
    private static final String TAG = RxUtil.class.getSimpleName();

    public static <T> void ioMain(Observable<T> observable, final NetNavigator<T> netNavigator) {
        //当我们获取数据的时候,这些方法的回调,如果是代理对象,执行success
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<T>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        netNavigator.start(netNavigator.getTitle());

                    }

                    @Override
                    public void onNext(T data) {
                        //请求成功
                        netNavigator.success(data);

                        Log.d(TAG, "onNext: 获取数据成功!:\n" + new Gson().toJson(data));
                    }

                    @Override
                    public void onError(Throwable e) {
                        //请求失败
                        netNavigator.fail(e.getMessage());
                        netNavigator.end();
                    }

                    @Override
                    public void onComplete() {
                        netNavigator.end();
                    }
                });
    }

}
